package com.imooc.bilibili.controller;

import com.imooc.bilibili.entity.VideoCoinsInfo;
import com.imooc.bilibili.entity.VideoCollectionInfo;
import com.imooc.bilibili.entity.VideoInfo;
import com.imooc.bilibili.entity.VideoView;
import com.imooc.bilibili.entity.page.PageResult;
import com.imooc.bilibili.es_service.ElasticsearchService;
import com.imooc.bilibili.result.Result;
import com.imooc.bilibili.service.VideoService;
import com.imooc.bilibili.support.UserSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user-video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private UserSupport userSupport;

    @Autowired
    private ElasticsearchService esService;

    /**
     * 视频投稿
     *
     * @param videoInfo
     * @return
     */
    @PostMapping("/videoes-insert")
    public Result<String> insertAddVideos(@RequestBody VideoInfo videoInfo) {
        Long userId = userSupport.getCurrnetUserId().get("userId");
        videoInfo.setUserId(userId);

        videoService.insertVideoService(videoInfo);
        //在es中添加视频数据
        esService.addVideo(videoInfo);
        return Result.success();
    }

    /**
     * @param size 每一页的数据量
     * @param no   页数
     * @param area 分区字段
     * @return
     */
    @GetMapping("/videos")
    public Result<PageResult<VideoInfo>> pageListVideos(Integer size, Integer no, String area) {
        Long userId = userSupport.getCurrnetUserId().get("userId");

        PageResult<VideoInfo> result = videoService.pageListVideos(size, no, area);
        return new Result<>(result);
    }

    /**
     * 视频在线观看
     *
     * @param request
     * @param response
     * @param url
     */
    @GetMapping("/video-slices")
    public void viewVedioOnlineSilices(HttpServletRequest request,
                                       HttpServletResponse response,
                                       String url) throws Exception {
        videoService.viewVedioOnlineSilicesService(request, response, url);
    }

    /**
     * 视频点赞
     */
    @PostMapping("/video-likes")
    public Result<String> insertVideoLike(@RequestParam Long videoId) {
        Long userId = userSupport.getCurrnetUserId().get("userId");
        videoService.insertVideoLikeService(videoId, userId);
        return Result.success();
    }

    /**
     * 取消点赞
     */
    @DeleteMapping("/video-unlike")
    public Result<String> deleteVideoLike(@RequestParam Long videoId) {
        Long userId = userSupport.getCurrnetUserId().get("userId");
        videoService.delteVideoLikeService(videoId, userId);
        return Result.success();
    }

    /**
     * 查询视频点赞数量
     */
    @GetMapping("/vdieo-like-num")
    public Result<Map<String, Object>> getVideoLike(@RequestParam Long videoId) {
        Long userId = null;
        try {
            userId = userSupport.getCurrnetUserId().get("userId");
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        Map<String, Object> result = videoService.getVideoLikeNumService(videoId, userId);
        return new Result<>(result);
    }

    /**
     * 视频收藏
     */
    @PostMapping("/video-collections-insert")
    public Result<String> insertVideoCollection(@RequestBody VideoCollectionInfo collectionInfo) {
        Long userId = userSupport.getCurrnetUserId().get("userId");
        collectionInfo.setUserId(userId);
        videoService.insertVideoCollectionService(collectionInfo);
        return Result.success();
    }

    /**
     * 取消视频收藏
     */
    @DeleteMapping("/video-collection-delete")
    public Result<String> deleteVideoCollection(@RequestParam Long videoId) {
        Long userId = userSupport.getCurrnetUserId().get("userId");
        videoService.deleteVideoColletionService(userId, videoId);
        return Result.success();
    }

    /**
     * 查看视频收藏数量
     */
    @GetMapping("/video-collection-num")
    public Result<Map<String, Object>> queryVideoCollectionNum(@RequestParam Long videoId) {
        Long userId = null;
        try {
            userId = userSupport.getCurrnetUserId().get("userId");
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        Map<String, Object> result = videoService.getVideoCollectionNumService(videoId, userId);
        return new Result<>(result);
    }

    /**
     * 视频投币
     */
    @PostMapping("/video-coins-insert")
    public Result<String> insertVideoCoins(@RequestBody VideoCoinsInfo videoCoinsInfo) {
        Long userId = userSupport.getCurrnetUserId().get("userId");
        videoService.insertVideoCoinsService(videoCoinsInfo);
        return Result.success();
    }

    /**
     * 统计投币数量
     */
    @GetMapping("/video-coins-count")
    public Result<Map<String, Object>> queryVideoCoinsCount(@RequestParam Long videoId) {
        Long userId = null;
        try {
            userId = userSupport.getCurrnetUserId().get("userId");
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        Map<String, Object> result = videoService.queryVideoCoinsCountService(userId, videoId);
        return new Result<>(result);
    }

    /**
     * 查询视频详情
     *
     * @param videoId
     * @return
     */
    @GetMapping("/video-details")
    public Result<Map<String, Object>> getVideoDetails(@RequestParam Long videoId) {
        Map<String, Object> result = videoService.getVideoDetailsService(videoId);
        return new Result<>(result);
    }

    /**
     * 添加观看记录
     */
    @PostMapping("/video-views")
    public Result<String> addVideoView(@RequestParam VideoView view,
                                       HttpServletRequest request) {
        Long userId;
        try {
            userId = userSupport.getCurrnetUserId().get("userId");
            view.setUserId(userId);
            videoService.insertVideoViewService(view, request);
        } catch (Exception exception) {
            videoService.insertVideoViewService(view, request);
        }
        return Result.success();
    }

    /**
     * 查询视频播放量
     */
    @GetMapping("/video-view-counts")
    public Result<Integer> getVideoViewCounts(@RequestParam Long videoId) {
        Integer counts = videoService.queryVideoViewCountsServicec(videoId);
        return new Result<>(counts);
    }

    /**
     * 用户在视频下方添加评论
     */


    /**
     * 用户删除评论
     */

    /**
     * 视频展示评论
     */

    /**
     * es查询视频数据
     */
    @GetMapping("/es-videds")
    public Result<VideoInfo> getEsVideos(@RequestParam String keyWord) {
        VideoInfo video = esService.getVideo(keyWord);
        return new Result<>(video);
    }

    /**
     * 视频内容推荐
     */
    @GetMapping("/recommend-ations")
    public Result<List<VideoInfo>> recommend() throws Exception {
        Long userId = userSupport.getCurrnetUserId().get("userId");
        List<VideoInfo> list = videoService.recommendService(userId);
        return new Result<>(list);
    }
}
